from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect,HttpResponse

from cardwall.greatwall.src import wall_ctrl
from cardwall.greatwall.src import stage_ctrl
from cardwall.greatwall.src import card_ctrl
from cardwall.greatwall.src import card_move_ctrl

class Views(object):

    def __init__(self):
        self.wall_ops = wall_ctrl.WallOperations()
        self.stage_ops = stage_ctrl.StageOperations()
        self.card_ops = card_ctrl.CardOperations()
        self.card_move_ops = card_move_ctrl.CardMoveOperations()

    def main_page(self, request):
        existing_walls = self.wall_ops.get_all_active_walls()
        return render_to_response('cardwall.html',
                                  {'existing_walls': existing_walls})

    def add_wall(self, request):
        title = request.POST['wall_title']
        owner = request.POST['wall_owner']
        description = request.POST['wall_desc']
        wall_obj = self.wall_ops.create_wall(title=title, owner=owner,
                                             descr=description)

        return HttpResponseRedirect('/cardwall/view_wall/' + str(wall_obj.id))

    def add_card(self, request, wall_id):
        if request.POST:
            title = request.POST['card_title']
            start_dt = request.POST['startdate']
            end_dt = request.POST['enddate']
            devs = request.POST['card_devs']
            points = request.POST['card_points']
            card_color = request.POST['card_color']
            desc = request.POST['card_desc']
            stage_obj = self.wall_ops.get_starting_stage(wall_id)
            self.card_ops.create_card(title=title, stage=stage_obj, start_dt=start_dt,
                                      end_dt=end_dt, devs=devs, desc=desc, card_color=card_color)
            return HttpResponse("<script>window.close();window.opener.location.reload();</script>")
        else:
            card_details = ''
            return render_to_response("edit_card.html",{"heading":"Add Card","wall_id":wall_id})

    def toogle_card(self, request, card_id):
        card = self.card_ops.get_card_by_id(card_id)
        self.card_ops.toogle_card(card)

    def view_wall(self, request, wall_id):
        wall_contents = self.wall_ops.get_wall_contents(wall_id=wall_id)
        stage_width = (100.0 / len(wall_contents)) - 1
        return render_to_response('view_wall.html', {'wall_contents': wall_contents,
                                                     'wall_id': wall_id,
                                                     'stage_width': stage_width})

    def move_card(self, request, card_id, new_stage_id):
        card = self.card_ops.get_card_by_id(card_id)
        new_stage = self.stage_ops.get_stage_by_id(new_stage_id)
        if card.fk_stage != new_stage:
            card.move(new_stage=new_stage)
            self.card_move_ops.save_move(card=card, new_stage=new_stage)

    def edit_stages(self, request):
        wall_id = request.POST['wall_id']
        stage_list = [request.POST['stage1'],
                      request.POST['stage2'],
                      request.POST['stage3'],
                      request.POST['stage4'],
                      request.POST['stage5'],
                      request.POST['stage6']]

        self.wall_ops.save_stages(wall_id=wall_id,
                                  stage_list=stage_list)
        return HttpResponseRedirect('/cardwall/view_wall/' + str(wall_id))

    def delete_card(self, request, card_id):
        wall_id = self.card_ops.delete_card(card_id)
        return HttpResponseRedirect('/cardwall/view_wall/' + str(wall_id))

    def edit_card(self, request, card_id):
        if card_id:
            card_details = self.card_ops.get_card_by_id(card_id)
            wall_id = self.card_ops.get_wall_by_stage_id(card_details.fk_stage_id)

        body_events = ''
        if request.POST:
            card_list = [request.POST['card_title'],
                         request.POST['startdate'],
                         request.POST['enddate'],
                         request.POST['card_devs'],
                         request.POST['card_points'],
                         request.POST['card_desc'],
                         request.POST['card_color']]

            self.card_ops.save_edit_card(card_id=card_id,
                                         card_list=card_list)

            return HttpResponse("<script>window.close();window.opener.location.reload();</script>")
                #return HttpResponseRedirect('/cardwall/view_wall/'+ str(wall_id))

        else:
            return render_to_response("edit_card.html",
                                      {'card_details':card_details,
                                       "heading":"Edit Card","wall_id":0})

    def card_comments(self, request, card_id):
        if request.POST:
            card_comment = request.POST['card_comments']
            self.card_ops.add_comments(card_id, card_comment)
        comments_list = self.card_ops.get_card_comments(card_id)
        return render_to_response("card_comments.html",
                                  {'comments':comments_list,'card_id':card_id})
